package com.example.service.impl;

import com.example.mapper.PayMapper;
import com.example.mapper.PlanMapper;
import com.example.mapper.ProductionMapper;
import com.example.mapper.TaskMapper;
import com.example.pojo.PageBean;
import com.example.pojo.Plan;
import com.example.pojo.Production;
import com.example.pojo.Task;
import com.example.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Service
public class TaskServiceImpl implements TaskService {
    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private PayMapper payMapper;
    @Autowired
    private PlanMapper planMapper;

    @Autowired
    private ProductionMapper productionMapper;
    @Override
    public void add(Task task) {
        taskMapper.add(task);
        List<Integer> ids = new ArrayList<>();
        ids.add(task.getPlanID());
        //planMapper.delete(ids);
    }

    @Override
    public PageBean page(Integer page, Integer pageSize, String taskName, String status, LocalDate begin, LocalDate end) {

        Long count = taskMapper.count(taskName, status, begin, end);//总数返回
        Integer start = (page - 1) * pageSize;//计算起始位置
        List<Task> empList = taskMapper.page(start, pageSize, taskName, status, begin, end);//该页记录数返回

        PageBean pageBean = new PageBean(count, empList);
        return pageBean;
    }

    @Override
    public void update(Task task) {
        int plan_id=task.getPlanID();
        taskMapper.update(task);
        if (task.getStatus().equals("已完成")&&taskMapper.selectno(plan_id)==taskMapper.selectyy(plan_id)) {

            int pay_id=planMapper.select(plan_id);
            Production production=payMapper.seletName(pay_id);
            production.setUnit("吨");
            if(productionMapper.select(production.getName())==0){
                productionMapper.add(production);
            }else{
                productionMapper.update(production);
            }
        }
    }

    @Override
    public List<Task> selectAll() {
        List<Task> taskList = taskMapper.selectAll();
        return taskList;
    }

    @Override
    public void updates(Task task) {
        taskMapper.updates(task);
    }

    @Override
    public PageBean pages(Integer page, Integer pageSize, String taskName, String status, String schedulingMode, String priorityRules, LocalDate begin, LocalDate end) {

        Long count = taskMapper.counts(taskName, status,schedulingMode,priorityRules,begin, end);//总数返回
        Integer start = (page - 1) * pageSize;//计算起始位置
        List<Task> empList = taskMapper.pages(start, pageSize, taskName, status,schedulingMode,priorityRules, begin, end);//该页记录数返回

        PageBean pageBean = new PageBean(count, empList);
        return pageBean;
    }

}
